package com.example.blogspring.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/5/22 0:38
 * 统一数据格式返回
 */
@Data
public class HttpResult implements Serializable {
    private Integer code;
    private String msg;
    private Object data;

    //    返回成功
    public static HttpResult success(Object data) {
        HttpResult result = new HttpResult();
        result.setCode(200);
        result.setMsg("");
        result.setData(data);
        return result;
    }

    public static HttpResult success(String msg, Object data) {
        HttpResult result = new HttpResult();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static HttpResult success(Integer code, String msg, Object data) {
        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    //    返回失败
    public static HttpResult fail(Integer code) {
        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setMsg("");
        result.setData(null);
        return result;
    }

    public static HttpResult fail(Integer code, String msg) {
        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }

    public static HttpResult fail(Integer code, String msg, Object data) {
        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
